/*AyflEomXWyqCHfdTxzWdaxlUNLw1FqwpfygTLUPuoIY=*/
import React, { Component } from 'react';
import {high,base,ajax } from 'nc-lightapp-front';
const {
    NCModal: Modal,
    NCButton: Button,
    NCTable: Table,
    NCCheckbox:Checkbox,
    NCSelect:Select,
    NCRow: Row,
    NCCol: Col,
} = base;

const NCOption = Select.NCOption;

import '../../../../css/referModal/index.less';
import ReferLoader from '../../../../../public/ReferLoader/index.js';
class NewModal extends Component{
    constructor(props){
        super(props);
        this.state = {
            tableData: [], //表格数据,用来渲染
            oldTableData: [], //表格初始数据,’取消‘用到
        }
        this.sureHandle = this.sureHandle.bind(this); //'确定'事件
        this.closeNewModal = this.closeNewModal.bind(this); //'取消'事件
    }
    componentWillReceiveProps(nextProps){
        let tempData = nextProps.record && nextProps.record.selectCellAttr ? nextProps.record.selectCellAttr : []
        let oldTableData = tempData.length > 0 ? JSON.parse(JSON.stringify(tempData)) : [];
        let searchparam = nextProps.searchparam
        this.setState({
            tableData: tempData,
            oldTableData: oldTableData,
            searchparam: searchparam
        })
    }
    componentDidMount(){
        this.props.NewModalRef(this);
    }
    secondModalSelect(record, index){//第二层弹框的复选框
        let newSelect = !record.select;
        record.select = newSelect;
    }
    onCompChange = (val, index, itemtype) => {
        let {tableData} = this.state;
        if(itemtype==='refer'){
            let refpkArr = [];
            let selectName = [];
            val.forEach((item) => {
                refpkArr.push(item.refpk);
                selectName.push(item.refname)
            })
            tableData[index].selectCells = val
            let pk_checkvalue = refpkArr.join(',')
            tableData[index].pk_checkvalue = pk_checkvalue;
            tableData[index].selectName = selectName;
            tableData[index].refpk = refpkArr;
        } else {
            tableData[index].supplierType = val; //'供应商类型'的值
            tableData[index].pk_checkvalue = val;
            tableData[index].selectName = val;
            tableData[index].refpk = val;
        } 
        this.setState({
            tableData
        })      
    }

    renderNewRefPath(record, index, fieldid){
        let mybook;
        let { buSecond, unitValueParam, accountingbook, isversiondate, versiondate, busiDate, json } = this.state.searchparam
        if(record.refpath){
                let pkOrgParam = buSecond.length > 0 ? buSecond[0].refpk : unitValueParam; // accountingbook[0].nodeData ? accountingbook[0].nodeData.pk_corp : accountingbook[0].pk_corp;//;
                let options = {
                    fieldid: fieldid,
                    refcode: record.refpath+".js",
                    value: record.selectCells,
                    isMultiSelectedEnabled: true,
                    "isShowDisabledData": true,
                    pk_defdoclist: record.classid? record.classid:"",
                    queryCondition: () => {
                        return {
                            "pk_accountingbook": accountingbook[0].pk_accountingbook ? accountingbook[0].pk_accountingbook : '',
                            "pk_org": pkOrgParam,
                            "isDataPowerEnable": 'Y',
                            "DataPowerOperationCode" : 'fi',
                            "dateStr": isversiondate ? versiondate : busiDate
                        }
                    },
                    onChange: (val) => {
                        this.onCompChange(val, index, 'refer')
                    }
                };
                let newOPtions = {};
                if(record.classid=='b26fa3cb-4087-4027-a3b6-c83ab2a086a9'||record.classid=='40d39c26-a2b6-4f16-a018-45664cac1a1f') {//部门，人员
                    newOPtions = {
                        ...options,
                        "unitValueIsNeeded":false,
                        "isShowDimission":true,
                        queryCondition: () => {
                            return {
                                "pk_accountingbook": accountingbook[0].pk_accountingbook ? accountingbook[0].pk_accountingbook : '',
                                "pk_org": pkOrgParam,
                                "isDataPowerEnable": 'Y',
                                "DataPowerOperationCode" : 'fi',
                                "busifuncode":"all",
                                isShowDimission:true
                            }
                        },
                        isShowUnit:true,
                        unitProps:{
                            refType: 'tree',
                            refName: json['20028001-000015'], ///* 国际化处理： 业务单元*/
                            refCode: 'uapbd.refer.org.BusinessUnitTreeRef',
                            rootNode:{refname:json['20028001-000015'],refpk:'root'}, ///* 国际化处理： 业务单元*/
                            placeholder:json['20028001-000015'], ///* 国际化处理： 业务单元*/
                            queryTreeUrl: '/nccloud/uapbd/org/BusinessUnitTreeRef.do',
                            treeConfig:{name:[json['20028001-000016'], json['20028001-000017']],code: ['refcode', 'refname']}, ///* 国际化处理： 编码,名称*/
                            isMultiSelectedEnabled: false,
                            //unitProps:unitConf,
                            isShowUnit:false
                        },
                        unitCondition:{
                            pk_financeorg: pkOrgParam,
                            'TreeRefActionExt':'nccloud.web.gl.ref.OrgRelationFilterRefSqlBuilder'
                        },
                    }
                }else if(record.classid && record.classid.length === 20){
                    newOPtions = {
                        ...options,
                        pk_defdoclist: record.classid
                    }
                }
                // else if(record.classid &&record.classid=='eae040f4-3c88-413d-abc9-b15774463d46'){
                //     newOPtions = {
                //         ...options,
                //         pk_defdoclist: record.classid
                //     }
                // }
                else {
                    newOPtions = {
                        ...options
                    }
                }
                mybook = (
                    <Row>
                        <Col xs={12} md={12}>
                            <ReferLoader {...newOPtions}/>
                        </Col>
                    </Row>
                );
            // }
		}else if(record.enums){
            return (
                <Select
                    fieldid={fieldid}
                    value={record.supplierType || ''} //supplierType '供应商类型'的值
                    onChange={(val) => {
                        this.onCompChange(val, index, 'select')
                    }}
                >
                    <NCOption value='' ></NCOption>
                    {record.enums.map((item, index) => {
                        return <NCOption value={item.value} key={index} >{item.name}</NCOption>
                    })}
                </Select>
            )
        }

        return mybook;
	}
    
    getColumn = (json) => {
        let secondColumn = [
            {
                title: (<div fieldid='select'>{json['20028001-000010']}</div>),/* 国际化处理： 选择*/
                dataIndex: "select",
                key: "select",
                width: '100px',
                render: (text, record, index) => {
                    return  <div fieldid='select'><Checkbox
                        checked={record.select}
                        onChange={() => {
                            record.index = index;
                            this.secondModalSelect(record, index);
                        }}
                    /></div>
                }
            },
            {
                title: (<div fieldid='name'>{json['20028001-000011']}</div>),/* 国际化处理： 查询属性*/
                dataIndex: "name",
                key: "name",
                width: '180px',
                render: (text, record, index) => <div fieldid='name'>{text ? text : <span>&nbsp;</span>}</div>
            },
            {
                title: (<div fieldid='refpath'>{json['20028001-000012']}</div>),/* 国际化处理： 查询值*/
                dataIndex: "refpath",
                key: "refpath",
                width: '180px',
                render: (text, record, index) => {
                    let renderSecondEle = this.renderNewRefPath(record, index, 'refpath');
                    return renderSecondEle;
                }
            }
        ];
        return secondColumn
    }
    sureHandle = () => {
        this.props.sureHandle(this.state.tableData);
    }
    closeNewModal = () => {
        this.props.closeNewModal(this.state.oldTableData)
    }
    // 打开弹框
    show = () => {
        this.props.modal.show('attributes');
    }
    // 关闭弹框
    close = () => {
        this.props.modal.close('attributes');
    }
    modalContent = () => {
        let { json } = this.props;
        let columns = this.getColumn(json)
        let { tableData } = this.state
        return(
            <Table
                columns={columns}
                data={tableData}
            />
        )
    }
    render(){
        let { title, closeNewModal, json, modal } = this.props;
        return(
            <div>
                {modal.createModal('attributes', {
					title: title, 
					content: this.modalContent(), //弹框内容，可以是字符串或dom
					beSureBtnClick: this.sureHandle,//点击确定按钮事件
                	cancelBtnClick: ()=>{closeNewModal && this.closeNewModal()}, //取消按钮事件
					closeModalEve: ()=>{closeNewModal && this.closeNewModal()}, //关闭按钮事件回调
				})}
            </div>
            
        )
    }
}

export default NewModal;

/*AyflEomXWyqCHfdTxzWdaxlUNLw1FqwpfygTLUPuoIY=*/